<?php
namespace app\common\utils;
use think\facade\Config;

class MyRedis
{

    private static $_instance;
    private $cache = null;

    public function __construct($short_connect = true)
    {
        $this->cache = new \Redis();
        $redisConfig = Config::get('REDIS_CONFIG');
        if($short_connect) {
            $this->cache->connect($redisConfig['host'], $redisConfig['port']);
        }else {
            $this->cache->pconnect($redisConfig['host'], $redisConfig['port']);
        }

        if(!empty($redisConfig['password'])){
            $this->cache->auth($redisConfig['password']);
        }
    }

    public static function instance(){
        if(!self::$_instance){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function __destruct()
    {
        $this->cache->close();
    }

    public function incr($key)
    {

        return $this->cache->incr($key);

    }

    public function decr($key)
    {

        return $this->cache->decr($key);

    }

    public function hIncrBy($key, $field, $value)
    {
        return $this->cache->hIncrBy($key, $field, $value);
    }

    public function hGet($key, $field)
    {
        return $this->cache->hGet($key, $field);
    }

    public function hMGet($key, $fields)
    {
        return $this->cache->hMGet($key, $fields);
    }

    public function hSet($key, $field, $value)
    {
        return $this->cache->hSet($key, $field, $value);
    }

    public function hSetNx($key, $field, $value)
    {
        return $this->cache->hSetNx($key, $field, $value);
    }

    public function hMSet($key, $hashKeys){
        return $this->cache->hMset($key, $hashKeys);
    }

    public function hGetAll($key)
    {
        return $this->cache->hGetAll($key);
    }

    public function hDel($key, $field)
    {
        return $this->cache->hDel($key, $field);
    }

    public function get($key)
    {
        return $this->cache->get($key);
    }

    public function mget($keys)
    {
        return $this->cache->mget($keys);
    }

    public function del($key)
    {
        $this->cache->del($key);
    }

    /**
     * 执行指令集流水线执行
     * @param $ins
     */
    public function pipeline()
    {
        $cache = $this->cache->multi(\Redis::PIPELINE);
        return $cache;
    }

    /**
     * 执行指令集流水线执行方式2
     * @param $user_function
     * @return mixed
     */
    public function multi($user_function)
    {
        $cache = $this->cache->multi(\Redis::PIPELINE);
        $args = func_get_args();
        array_shift($args);
        array_unshift($args, $cache);
        call_user_func_array($user_function, $args);
        $ret = $cache->exec();
        return $ret;
    }

    public function set($key, $value, $expire = 86400, $options = 'ex')
    {
        if (-1 == $expire) {
            return $this->cache->set($key, $value);
        }
        if (empty($options)) {
            $options = 'ex';
        }
        if (in_array($options, array('ex', 'px'))) {
            return $this->cache->set($key, $value, Array($options => $expire));
        } elseif (in_array($options, array('nx', 'xx'))) {
            return $this->cache->set($key, $value, Array($options));
        } else {
            return $this->cache->set($key, $value, Array('ex' => $expire));
        }
    }

    public function setnx($key, $value)
    {
        return $this->cache->setnx($key, $value);
    }

    public function setex($key, $ttl,$value)
    {
        return $this->cache->setex($key, $ttl,$value);
    }

    public function zAdd($key, $score, $value)
    {
        return $this->cache->zAdd($key, $score, $value);
    }

    public function sAdd($key, $value)
    {
        return $this->cache->sAdd($key, $value);
    }

    public function sRem($key, $value)
    {
        return $this->cache->sRem($key, $value);
    }

    public function sMembers($key)
    {
        return $this->cache->sMembers($key);
    }

    public function sRandMember($key)
    {
        return $this->cache->sRandMember($key);
    }

    public function zRank($key, $member)
    {
        return $this->cache->zRank($key, $member);
    }

    public function zRevRank($key, $member)
    {
        return $this->cache->zRevRank($key, $member);
    }

    public function zScore($key, $member)
    {
        return $this->cache->zScore($key, $member);
    }

    public function zRange($key, $start, $end, $withScore = null)
    {
        return $this->cache->zRange($key, $start, $end, $withScore);
    }

    public function zRem($key, $member)
    {
        return $this->cache->zRem($key, $member);
    }

    public function zRevRange($key, $start, $end, $withScore = null)
    {
        return $this->cache->zRevRange($key, $start, $end, $withScore);
    }

    public function zRangeByScore($key, $start, $end, $option = array()){
        return $this->cache->zRangeByScore($key, $start, $end, $option);
    }

    public function zIncrBy($key, $member, $value)
    {
        return $this->cache->zIncrBy($key, $member, $value);
    }

    public function info($option)
    {
        return $this->cache->info($option);
    }

    public function setTimeout($key, $ttl)
    {
        $this->cache->setTimeout($key, $ttl);
    }

    public function zRemRangeByRank($key, $start, $end)
    {
        return $this->cache->zRemRangeByRank($key, $start, $end);
    }

    public function zRevRangeByScore($key, $start, $end, array $options = array())
    {
        return $this->cache->zRevRangeByScore($key, $start, $end, $options);
    }

    public function zRemRangeByScore($key, $start, $end )
    {
        return $this->cache->zRemRangeByScore($key, $start, $end);
    }

    public function zCount($key, $start, $end)
    {
        return $this->cache->zCount($key, $start, $end);
    }

    public function exists($key){
        return $this->cache->exists($key);
    }

    public function scan(&$iterator, $pattern = null, $count = 0)
    {
        return $this->cache->scan($iterator, $pattern, $count);
    }


    public function type( $key ) {
        return $this->cache->type($key);
    }

    public function strLen($key){
        return $this->cache->strlen($key);
    }

    public function lLen($key){
        return $this->cache->lLen($key);
    }

    public function sCard($key){
        return $this->cache->sCard($key);
    }

    public function hLen($key){
        return $this->cache->hLen($key);
    }

    public function zCard($key){
        return $this->cache->zCard($key);
    }

    public function keys($pattern){
        return $this->cache->keys($pattern);
    }

    public function hExists($key,$field){
        return $this->cache->hExists($key,$field);
    }

    public function lPush($key,$value){
        return $this->cache->lPush($key, $value);
    }

    public function rPop($key){
        return $this->cache->rPop($key);
    }

    public function sIsMember($key,$value){
        return $this->cache->sIsMember($key,$value);
    }

    public function expire($key,$ttl){
        return $this->cache->expire($key,$ttl);
    }
}
